package ece452.ims.view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//import javax.mail.internet.*;
import ece452.ims.connection.*;

public class CreateAccountWindow extends JPanel {
	
	protected JTextField unameField; 
	protected JPasswordField pwdField, confirmPwdField;
	protected JButton caButton, loginButton;
	private static Relay rl;
	private String pwds;
	
	public CreateAccountWindow(Relay r){
		super(new GridLayout(6,1));
		this.rl = r;
		// Text Fields
		unameField = new JTextField(15);
		pwdField = new JPasswordField(15);
		confirmPwdField = new JPasswordField(15);
		
		// Labels
		JComponent header = makeTextPanel("Create an account");
		JComponent unameLabel = makeTextPanel("Username: ");
		JComponent pwdLabel = makeTextPanel("Password: ");
		JComponent confirmPwdLabel = makeTextPanel("Confirm password: ");
		
		// Buttons
		caButton = new JButton("Create Account");
		caButton.setEnabled(true);
		caButton.setActionCommand("Create an account");
		caButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String un = unameField.getText();
				char[] pwdc = pwdField.getPassword();
				char[] vpwdc = confirmPwdField.getPassword(); 
				String vpwds; pwds = vpwds = "";
				for(int i = 0; i < pwdc.length; ++i) pwds+=pwdc[i];
				for(int i=0; i < vpwdc.length; ++i) vpwds+=vpwdc[i];
				if(!(pwds.equals(vpwds))){
					displayMessage(3);
				}else{ 
					rl.createAccount(unameField.getText(), pwds);
					loginButton.setEnabled(true);
					System.out.println("aah");
					displayMessage(4);
				}
			}
		});
		
		loginButton = new JButton("Login");
		loginButton.setEnabled(false);
		loginButton.setActionCommand("Login to the service.");
		loginButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				rl.login(unameField.getText(), pwds);
			}
		});
		
		/* Panels */
		// padding: top, left, bottom, right
		// Header Label
		JPanel headerPanel = new JPanel();
		headerPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		headerPanel.add(header);
		headerPanel.setBorder(BorderFactory.createEmptyBorder(20,0,0,0));
		// Username Label
		JPanel unamePanel = new JPanel();
		unamePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		unamePanel.add(unameLabel);
		unamePanel.add(unameField);
		unamePanel.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		// Password Label
		JPanel pwdPanel = new JPanel();
		pwdPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		pwdPanel.add(pwdLabel);
		pwdPanel.add(pwdField);
		pwdPanel.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		// Confirm Password Label
		JPanel confirmPwdPanel = new JPanel();
		confirmPwdPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		confirmPwdPanel.add(confirmPwdLabel);
		confirmPwdPanel.add(confirmPwdField);
		confirmPwdPanel.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		// Login button
		JPanel loginButtonPanel = new JPanel();
		loginButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		loginButtonPanel.add(loginButton);
		loginButtonPanel.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		// Create Account Button
		JPanel caPanel = new JPanel();
		caPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		caPanel.add(caButton);
		caPanel.setBorder(BorderFactory.createEmptyBorder(15,0,0,0));
		
		add(headerPanel);
		add(unamePanel);
		add(pwdPanel);
		add(confirmPwdPanel);
		add(caPanel);
		add(loginButtonPanel);
	}
	
	protected JComponent makeTextPanel(String text) {
		JPanel panel = new JPanel(true);
		JLabel filler = new JLabel(text);
		filler.setHorizontalAlignment(JLabel.CENTER);
		panel.setLayout(new GridLayout(1, 1));
		panel.add(filler);
		return panel;
	}
	
	private void displayMessage(int n){
		// series of messageboxes 
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		JComponent message;
		JButton ok = new JButton("OK");
		switch(n){
		case 1:	// account already exists
			message = makeTextPanel("You must enter a valid User ID.");
			break;
		case 2:
			message = makeTextPanel("You must enter a valid password.");
			break;
		case 3:
			message = makeTextPanel("The two passwords do not match.");
			break;
		case 4:
			message = makeTextPanel("You may now login!");
			break;
		case 5:
			message = makeTextPanel("Default error message.");
			break;
		default:
			message = makeTextPanel("Default error message.");
			break;
		}
		JPanel okButton = new JPanel();
		okButton.add(ok);
		okButton.setLayout(new FlowLayout(FlowLayout.CENTER));
		okButton.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
		final JFrame errorFrame = new JFrame("Error: Does Not Compute");
		errorFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		errorFrame.setPreferredSize(new Dimension(450, 200));
		errorFrame.setLocation(screenSize.height/2, screenSize.width/3);
		errorFrame.getContentPane().add(message, BorderLayout.CENTER);
		errorFrame.getContentPane().add(okButton, BorderLayout.PAGE_END);
		errorFrame.pack();
		errorFrame.setVisible(true);
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				errorFrame.setVisible(false);
			}
		});
	}
	
	/* For testing purposes only */
	public static void showGUI() {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		JFrame frame = new JFrame("Instant Messaging Service Login");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setPreferredSize(new Dimension(200, 350));
		frame.setLocation(screenSize.height/2, screenSize.width/6);
		frame.getContentPane().add(new CreateAccountWindow(null), BorderLayout.CENTER);
		frame.pack();
		frame.setVisible(true);
		frame.setResizable(false);
	}

	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() { showGUI(); }
		});
	}
}
